package com.koron.order.overhaul.notified;

import com.koron.order.overhaul.mapper.OverhaulOrderMapper;
import com.koron.order.overhaul.utils.Constants;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;

/**
 * @author twcao
 * @title: OverhaulOrderNotify
 * @projectName GDH_EAM_V2
 * @description: 大修订单通知
 * @date 2021/9/1410:33
 */
public interface OverhaulOrderNotify<T> extends Ordered {

    default boolean execute(SessionFactory factory, T vo) {
        return true;
    }

    default boolean filter(SessionFactory factory, T vo) {
        return status().equals(getOldStatus(factory, vo));
    }

    Logger logger = LoggerFactory.getLogger(OverhaulOrderNotify.class);

    String status();

    /**
     * 默认工单id属性为"id"
     * @return
     */
    default String getOrderId() {
        return "id";
    }

    default String getOldStatus(SessionFactory factory, T vo) {
        try {
            String orderId = BeanUtils.getProperty(vo, getOrderId());
            if(StringUtils.isBlank(orderId)) {
                return Constants.OverhaulStatus.DRAFT;
            }
            OverhaulOrderMapper mapper = factory.getMapper(OverhaulOrderMapper.class);
            String currentStatus = mapper.getOrderStatus(orderId);
            return currentStatus == null ? Constants.OverhaulStatus.DRAFT : currentStatus;
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return null;
        }
    }
}
